const mgdb = require('../data/config')
const UserModel = require('./UserModel')
const CommentSchma = mgdb.Schema({ username: String, movieId: String, commentList: Array })
const Comment = mgdb.model('Comment', CommentSchma, 'comment_list')
const userModel = new UserModel()
class CommentModel {
    async publish({ username, movieId, commentObj }) {
        try {
            let userObj =await userModel.check({username})
            let list =await Comment.find({ username, movieId }).exec()
            // console.log(list);
            if (list.length > 0 && list) {
                list[list.length - 1].commentList.push({commentObj,userObj})
                let data = await list[list.length - 1].save()
                return data
            } else {
                let commentList = []
                commentList.push({commentObj,userObj})
                let info = new Comment({ username, movieId, commentList })
                let data = await info.save()
                return data
            }

        } catch (error) {
            console.log(error);
        }
    }
    async list({username,movieId}){
        try {
            let info = await Comment.find({username,movieId}).exec()
            return info
        } catch (error) {
            console.log(error);
        }
    }
}
module.exports = CommentModel